home *** CD-ROM | disk | FTP | other *** search
/ Nothing but Tetris / Nothing but Tetris.iso / tools / amiga / twinopus / dopus / readdir.rexx < prev    next >
Encoding:
OS/2 REXX Batch file  |  1995-07-15  |  10.2 KB  |  354 lines

  1. /*
  2.  *
  3.  * Get the contents of a directory from TwinExpres from DOpus.
  4.  *
  5.  * (c) 1994 by K.P. van Beem (2:280/464.2, patrick.van.beem@aobh.xs4all.nl)
  6.  *
  7.  * Based on the DOpusLhaARexx package by Geoff Seeley (but you can better
  8.  * use GuiArc in stead of DOpus and a script, to deal with archives)
  9.  
  10.      ---- modified by Ray Abram
  11.           - Bug Fix...   If a Device is requested to go into a dir on the remote
  12.                          that is not there, then the Twin REXX program
  13.                          will hang...
  14.                           ie:- a dir of DH0: on a CD32...
  15.             - solution   try a CD to the remote, if an error occurs then exit
  16.  
  17.           - File sorting, with dirs at the top and files at the bottom..
  18.  
  19.           - Caching
  20.                - if a file is found with the same name as the requested
  21.                  dir to read in, then that file is used to get the info from
  22.                - Use ReRead.rexx to delete the dir files if you need to reread
  23.                  dir listings
  24.                - note the files stored by the caching system replace
  25.                         : (a device name)   -> ;
  26.                         / (a dir seperator) -> \
  27.                   *** thus if a dir name contains these characters, then the caching
  28.                       logic will become confused and not work...
  29.                - now sorts dirs from files as the dir info from Twin is read in
  30.  *
  31.  */
  32.  
  33. DOpusPort   = 'DOPUS.1'
  34. HandlerPort = 'TWIN.1'
  35.  
  36. userdata     = '  0'      /* default      */
  37. fgpen        = '  1'      /* palette 1    */
  38. dirpen       = '  3'      /* for directories */
  39. bgpen        = '  0'      /* palette 2    */
  40. selectable   = ' 1'       /* can select   */
  41. unselectable = ' 0'       /* can't select */
  42. show         = ' 0'       /* update win   */
  43. before       = ' -1'      /* add to end   */
  44.  
  45. if ~show(l,"rexxsupport.library") then        
  46.     call addlib("rexxsupport.library",0,-30,0)
  47. if showlist('Ports', DOpusPort) = 0 then do
  48.    say 'Directory Opus Arexx port not found. Aborting.'
  49.    call CleanUp
  50. end
  51.  
  52. address 'DOPUS.1'
  53. options results
  54.  
  55. trace ?results
  56.  
  57. /* Get some information from DOpus */
  58. parse arg FilePath
  59. if FilePath="" then do
  60.    TopText "You have to specify a directory. !!"
  61.    exit
  62. end
  63. Status 3
  64. CurrentWindow = Result
  65.  
  66. /* check for spaces in filename */
  67. if words(FilePath) > 1 then do
  68.    Request "Spaces in a Filename are not Allowed !!"
  69.    exit
  70. end
  71.  
  72. /* Caching addition
  73.     check for the caching file,
  74.       if not found then make the file
  75.       if it is present then use the file
  76. */
  77.  
  78. CacheFile = 'TwinDirs:' || ConvertFilename(FilePath)
  79.  
  80. /* check for the dir already stored */
  81. if exists(CacheFile) then
  82.    Cach=True
  83.  else
  84.    Cache=False
  85.  
  86. if Cache=False then do
  87.    if left(FilePath,1) = '~' then
  88.       pt = "Remote has"
  89.    else
  90.       pt = "that there are"
  91.  
  92.    /*check that the remote directory exists ??? */
  93.    TOPTEXT "Checking to see if" FilePath "Exists ... (Ensure" pt "no Requestors !!)"
  94.    sd = EnterDir(FilePath)
  95.    address command 'echo >PPipe: dir ' sd
  96.    address command 'echo >PPipe: help'
  97.    if open(PipeList, "QUEUE:Twin", 'R') then do
  98.       /* skip the header and the 'help-flush' from the last call... */
  99.       junk = ''
  100.       test = ''
  101.       do while ((test ~= "TWIN> D") & (test ~= "TWIN> E") & (test ~= "Error: ") & (test ~= "Deafult"))
  102.          junk = readln(PipeList)
  103.          test = left(junk , 7, '')
  104.       end
  105.       close(PipeList)
  106.       if ((test = "TWIN> E") | (test = "Error: ")) then do
  107.          parse var test left '~' right
  108.          if right ~= '' then
  109.             Request "Local " FilePath "NOT FOUND ????"
  110.          else
  111.             Request "Remote " FilePath "NOT FOUND ????"
  112.          
  113.          TopText 'Ready'
  114.  
  115.          Busy off
  116.          exit
  117.       end
  118.    end
  119.    else do
  120.       TopText "Can't open pipe:"
  121.       call CleanUp
  122.     end
  123. end
  124.  
  125.  
  126. /* setup DOpus window and tell user what's happening */
  127. ClearWin CurrentWindow
  128. SetWinTitle FilePath
  129. Busy on
  130. TopText "Getting directory , Please Wait..."
  131.  
  132. /* Address the list command and a help to 'flush' the queue buffer
  133.  * never use a queue with a bigger buffer than the default buffer.
  134.  * The queue-handler doesn't flush automatically!
  135.  */
  136. if Cache=False then do
  137.    address command 'echo >PPipe: dir' FilePath
  138.    address command 'echo >PPipe: help'
  139. end
  140.  
  141. /* parse the result from pipe: */
  142. TopText "Parsing File(s). Please Wait..."
  143. call ParseDir
  144. 'DisplayDir -1'
  145.  
  146. /* if handler is running, attach a custom handler to the window */
  147. if show('p', HandlerPort) then
  148.    'AddCustHandler '||HandlerPort||' -1'
  149.  
  150. Busy off
  151. call CleanUp
  152. exit
  153.  
  154. /*---------------------------------------------------------------------------*/
  155.  
  156. ParseDir:
  157.  
  158.    if open(PipeList, "QUEUE:Twin", 'R') then do
  159.  
  160.       if Cache=False then do
  161.         /* skip the header and the 'help-flush' from the last call... */
  162.          junk = ''
  163.          do while left(junk, 10, '') ~= "Listing of"
  164.             junk = readln(PipeList)
  165.          end
  166.          junk = readln(PipeList)
  167.  
  168.         /* read files and dirs and save them to separate temp files*/
  169.          open(dirs,  "T:twin.dirs" , 'W')
  170.          open(files, "T:twin.files", 'W')
  171.  
  172.          line    = readln(PipeList)
  173.          lines   = 0
  174.          f_count = 0
  175.          d_count = 0
  176.          do while length(line) ~= 0
  177.              if SubStr(line,26,9) = 'Directory' then do
  178.                writeln(dirs,  line)
  179.                d_count = d_count + 1
  180.              end
  181.             else do
  182.                writeln(files, line)
  183.                f_count = f_count + 1
  184.             end
  185.  
  186.             /* get the next line */
  187.             lines = lines + 1
  188.             line = readln(PipeList)
  189.          end
  190.          close(dirs )
  191.          close(files)
  192.  
  193.          /* only sort Dirs and files if there is something to sort ... */
  194.          if lines > 0 then do
  195.  
  196.              /*write data out to the cache file as it is read in...*/
  197.              open(cache, CacheFile, 'W')
  198.  
  199.              /*sort the Dirs and store then into DOPus*/
  200.              if d_count > 0 then do
  201.                 /* tell user what we are doing */
  202.                 TOPTEXT "Sorting Directories..."
  203.  
  204.                 address command "sort t:twin.dirs  t:twin.dirs2"
  205.                /*read in the sorted data  dirs then files... */
  206.                 open(dirs,  "T:twin.dirs2" , 'R')
  207.                 line = readln(dirs)
  208.                 do while length(line) ~= 0
  209.                    writeln(cache , line)  /* store to cache file*/
  210.                    File = Quote(line)
  211.                    Entry = File || userdata || dirpen || bgpen || selectable || show || before
  212.                    AddCustEntry Entry
  213.                    line = readln(dirs)
  214.                 end
  215.                 close(dirs)
  216.              end
  217.  
  218.              /*sort the Files and store then into DOPus*/
  219.              if f_count > 0 then do
  220.                 /* tell user what we are doing */
  221.                 TOPTEXT "Sorting Files..."
  222.  
  223.                 address command "sort t:twin.files t:twin.files2"
  224.                 open(files,  "T:twin.files2" , 'R')
  225.                 line = readln(files)
  226.                 do while length(line) ~= 0
  227.                    writeln(cache , line)  /* store to cache file*/
  228.                    File = Quote(line)
  229.                    Entry = File || userdata || fgpen || bgpen || selectable || show || before
  230.                    AddCustEntry Entry
  231.                    line = readln(files)
  232.                 end
  233.                 close(files)
  234.              end
  235.  
  236.              close(cache)
  237.  
  238.              /*and now delete all temp files */
  239.              address command "delete t:twin.dirs"
  240.              address command "delete t:twin.dirs2"
  241.              address command "delete t:twin.files"
  242.              address command "delete t:twin.files2"
  243.          end /* if there are some files */
  244.  
  245.          close(PipeList)
  246.       end
  247.       else do
  248.          /*read in the stored cache file...*/
  249.          TOPTEXT "Loading Cached Information..."
  250.          open(cache, CacheFile, 'R')
  251.          line = readln(cache)
  252.          do while length(line) ~= 0
  253.             File = Quote(line)
  254.             if SubStr(line,26,9) = 'Directory' then
  255.                Entry = File || userdata || dirpen || bgpen || selectable || show || before
  256.             else
  257.                Entry = File || userdata || fgpen || bgpen || selectable || show || before
  258.             AddCustEntry Entry
  259.             line = readln(cache)
  260.          end
  261.          close(cache)
  262.       end
  263.  
  264.       /* add invisible entry giving us the path for recognition of the twin dir*/
  265.       File = Quote('*'||FilePath)
  266.       Entry = File || userdata || bgpen || bgpen || unselectable || show || before
  267.       AddCustEntry Entry
  268.  
  269.    end
  270.    else do
  271.       TopText "Can't open pipe:"
  272.       call CleanUp
  273.    end
  274.  
  275. return
  276.  
  277. /*---------------------------------------------------------------------------*/
  278.  
  279. CleanUp:
  280.  
  281.    TopText "Ready"
  282.    Busy off
  283.    exit
  284.  
  285. return
  286.  
  287. /*---------------------------------------------------------------------------*/
  288.  
  289. Quote: procedure /* add quotes to string */
  290.  
  291.    parse arg string
  292.  
  293. return '"'||string||'"'
  294.  
  295. /*---------------------------------------------------------------------------*/
  296.  
  297. /* convert a file name from df0:ray/was/here  ->  df0=ray\was\here
  298.                                     then to   ->  d0=ry\ws\he      (cache filename)*/
  299. ConvertFilename: procedure
  300.  
  301.    parse arg Dev ':' Path
  302.  
  303.    if left(dev,1) = '~' then
  304.       dev = '`' || right(dev,length(dev)-1)
  305.  
  306.    t2 = ''
  307.    Convert = ''
  308.    do until (t2 = '')
  309.        parse var Path t1 '/' t2
  310.        Path = t2
  311.        if t2 ~= '' then
  312.          end = '\'
  313.        else
  314.          end = ''
  315.        Convert = Convert || left(t1,1) || right(t1,1) || end
  316.    end
  317.  
  318.    if left(Convert,1) = ' ' then
  319.       Convert = ''
  320.  
  321.    out = Dev || '=' || Convert
  322.  
  323. return out
  324.  
  325. /*--------------------------------------------------------------------------*/
  326.  
  327. /* Cd into the sent Directory path
  328.    - this is needed, as TWIN has a command parameter limit of 30 characters,
  329.       and as we all know AmigaDos file & paths can easily go over this limit !!*/
  330.  
  331. EnterDir: procedure
  332.  
  333.    parse arg Dev ':' Path
  334.  
  335.    /* get the 1st character to address the local or remote machine correctly */
  336.    if left(Dev,1) = '~' then
  337.       sbit = '~'
  338.      else
  339.       sbit = ''
  340.  
  341.    /* does the passed name have a DEVICE in it ? */
  342.    if Dev ~= '' then
  343.       address command 'echo >PPipe: cd' Dev || ':'
  344.  
  345.    do until (t2 = '')
  346.       parse var Path t1 '/' t2
  347.       path = t2
  348.       if t1 ~= '' then
  349.          address command 'echo >PPipe: cd' sbit || t1
  350.    end
  351.  
  352. return sbit
  353.  
  354.